package com.lzh.persevere.controller;

import com.lzh.persevere.annotation.ViewCount;
import com.lzh.persevere.common.R;
import com.lzh.persevere.common.ResultUtil;
import com.lzh.persevere.model.entity.ArticleInfo;
import com.lzh.persevere.model.qo.qo.ArticlePageQO;
import com.lzh.persevere.model.ro.IdRO;
import com.lzh.persevere.model.ro.article.AddArticleRO;
import com.lzh.persevere.model.vo.article.ArticleDetailVO;
import com.lzh.persevere.model.vo.article.CategoryArticleVO;
import com.lzh.persevere.model.vo.tag.ArticleTagInfoVO;
import com.lzh.persevere.service.ArticleInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 文章controller
 *
 * @author luzhiheng
 * @since 2025-07-15
 */
@Api(tags = "文章")
@RestController
@RequestMapping("/articleInfo")
public class ArticleInfoController {

    @Resource
    private ArticleInfoService articleInfoService;

    @ApiOperation(value = "分页查询文章列表")
    @PostMapping("/findArticlePage")
    public R<Map<String, Object>> findArticlePage(@Valid @RequestBody ArticlePageQO qo) {
        return ResultUtil.success(articleInfoService.pageList(qo));
    }

    @ApiOperation(value = "查询每个分类的文章")
    @PostMapping("/findAllCategoryArticleList")
    public R<List<CategoryArticleVO>> findAllCategoryArticleList() {
        return ResultUtil.success(articleInfoService.findAllCategoryArticleList());
    }

    @ApiOperation(value = "查询推荐文章")
    @PostMapping("/findRecommendArticle")
    public R<List<ArticleInfo>> findRecommendArticle() {
        return ResultUtil.success(articleInfoService.findRecommendArticle());
    }

    @ViewCount(idParam = "id")
    @ApiOperation(value = "查询文章详情")
    @PostMapping("/findArticleDetail/{id}")
    public R<ArticleDetailVO> findArticleDetail(@PathVariable Long id) {
        return ResultUtil.success(articleInfoService.findArticleDetail(id));
    }

    @ApiOperation(value = "保存文章")
    @PostMapping("/addArticle")
    public R<Boolean> addArticle(@Valid @RequestBody AddArticleRO ro) {
        return ResultUtil.success(articleInfoService.addArticle(ro));
    }

    @ApiOperation(value = "更新文章")
    @PostMapping("/editArticle")
    public R<Boolean> editArticle(@Valid @RequestBody AddArticleRO ro) {
        return ResultUtil.success(articleInfoService.editArticle(ro));
    }

    @ApiOperation(value = "点赞文章")
    @PostMapping("/likeArticle")
    public R<Boolean> likeArticle(@Valid @RequestBody IdRO ro) {
        return ResultUtil.success(articleInfoService.likeArticle(ro.getId()));
    }

    @ApiOperation(value = "收藏文章")
    @PostMapping("/collectArticle")
    public R<Boolean> collectArticle(@Valid @RequestBody IdRO ro) {
        return ResultUtil.success(articleInfoService.collectArticle(ro.getId()));
    }

    @ApiOperation(value = "查询该分类下的文章的所有标签")
    @PostMapping("/findTagListByCategory")
    public R<List<ArticleTagInfoVO>> findTagListByCategory(@Valid @RequestBody IdRO ro) {
        return ResultUtil.success(articleInfoService.findTagListByCategory(ro.getId()));
    }

}
